package com.sc.nft.entity.dto;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.enums.MallGoodsLabelEnums;
import com.sc.nft.exception.GlobalRunTimeException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.http.client.utils.DateUtils;
import org.web3j.utils.Convert;

import javax.validation.constraints.NotNull;
import java.util.Date;

@Data
public class MallGoodsLabelDTO {
    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "权重")
    @NotNull(message = "权重不能为空")
    private Integer sort;

    @ApiModelProperty(value = "标签类型, NEW_LABEL,新品标签,ACTIVITY_LABEL,活动标签")
    @NotNull(message = "标签类型不能为空")
    private MallGoodsLabelEnums labelType;

    @ApiModelProperty(value = "标签名称")
    @NotNull(message = "标签名称不能为空")
    private String labelName;

    @ApiModelProperty(value = "标签图片")
    @NotNull(message = "标签图片不能为空")
    private String labelImage;

    @ApiModelProperty(value = "持续时间")
    private Integer duration;

    @ApiModelProperty("开始时间")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date startTime;

    @ApiModelProperty("结束时间")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date endTime;

    public void check() {
        if (labelType == MallGoodsLabelEnums.NEW_LABEL) {
            if (duration == null) {
                throw new GlobalRunTimeException("持续时间不能为空");
            }
            startTime = new Date();
            endTime = DateUtil.offsetDay(startTime,duration);
        }else if (labelType == MallGoodsLabelEnums.ACTIVITY_LABEL) {
            if (startTime == null && endTime == null) {
                throw new GlobalRunTimeException("开始时间，结束时间不能为空");
            }
            long day = DateUtil.betweenDay(startTime, endTime, false);
            duration = new Long(day).intValue();
        }
    }
}
